﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MapEditor.Forms
{
    public partial class Tiles : Form
    {
        public int TileClicked;
        public Point LocalMousePosition;

        
        public Tiles()
        {
            if (Game1.drawableLayer == 0)
            {
                byte[] textureData = new byte[4 * Game1.Front1TileSheet.Width * Game1.Front1TileSheet.Height];
                Game1.Front1TileSheet.GetData<byte>(textureData);
                System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(Game1.Front1TileSheet.Width, Game1.Front1TileSheet.Height,
                                System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, Game1.Front1TileSheet.Width, Game1.Front1TileSheet.Height),
                                        System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                IntPtr safePtr = bmpData.Scan0;
                System.Runtime.InteropServices.Marshal.Copy(textureData, 0, safePtr, textureData.Length);
                bmp.UnlockBits(bmpData);


                InitializeComponent();
                PictureBoxTiles.BackgroundImage = bmp;
                PictureBoxTiles.Height = Game1.Front1TileSheet.Height;
                PictureBoxTiles.Width = Game1.Front1TileSheet.Width;
            }
            if (Game1.drawableLayer == 1)
            {
                byte[] textureData = new byte[4 * Game1.Front2TileSheet.Width * Game1.Front2TileSheet.Height];
                Game1.Front2TileSheet.GetData<byte>(textureData);
                System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(Game1.Front2TileSheet.Width, Game1.Front2TileSheet.Height,
                                System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, Game1.Front2TileSheet.Width, Game1.Front2TileSheet.Height),
                                        System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                IntPtr safePtr = bmpData.Scan0;
                System.Runtime.InteropServices.Marshal.Copy(textureData, 0, safePtr, textureData.Length);
                bmp.UnlockBits(bmpData);


                InitializeComponent();
                PictureBoxTiles.BackgroundImage = bmp;
                PictureBoxTiles.Height = Game1.Front2TileSheet.Height;
                PictureBoxTiles.Width = Game1.Front2TileSheet.Width;
            }
            if (Game1.drawableLayer == 2)
            {
                byte[] textureData = new byte[4 * Game1.Front3TileSheet.Width * Game1.Front3TileSheet.Height];
                Game1.Front3TileSheet.GetData<byte>(textureData);
                System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(Game1.Front3TileSheet.Width, Game1.Front3TileSheet.Height,
                                System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, Game1.Front3TileSheet.Width, Game1.Front3TileSheet.Height),
                                        System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                IntPtr safePtr = bmpData.Scan0;
                System.Runtime.InteropServices.Marshal.Copy(textureData, 0, safePtr, textureData.Length);
                bmp.UnlockBits(bmpData);


                InitializeComponent();
                PictureBoxTiles.BackgroundImage = bmp;
                PictureBoxTiles.Height = Game1.Front3TileSheet.Height;
                PictureBoxTiles.Width = Game1.Front3TileSheet.Width;
            }
            if (Game1.drawableLayer == 3)
            {
                byte[] textureData = new byte[4 * Game1.PlayerTileSheet.Width * Game1.PlayerTileSheet.Height];
                Game1.PlayerTileSheet.GetData<byte>(textureData);
                System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(Game1.PlayerTileSheet.Width, Game1.PlayerTileSheet.Height,
                                System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, Game1.PlayerTileSheet.Width, Game1.PlayerTileSheet.Height),
                                        System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                IntPtr safePtr = bmpData.Scan0;
                System.Runtime.InteropServices.Marshal.Copy(textureData, 0, safePtr, textureData.Length);
                bmp.UnlockBits(bmpData);


                InitializeComponent();
                PictureBoxTiles.BackgroundImage = bmp;
                PictureBoxTiles.Height = Game1.PlayerTileSheet.Height;
                PictureBoxTiles.Width = Game1.PlayerTileSheet.Width;
            }
            if (Game1.drawableLayer == 4)
            {
                byte[] textureData = new byte[4 * Game1.EnemyTileSheet.Width * Game1.EnemyTileSheet.Height];
                Game1.EnemyTileSheet.GetData<byte>(textureData);
                System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(Game1.EnemyTileSheet.Width, Game1.EnemyTileSheet.Height,
                                System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, Game1.EnemyTileSheet.Width, Game1.EnemyTileSheet.Height),
                                        System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                IntPtr safePtr = bmpData.Scan0;
                System.Runtime.InteropServices.Marshal.Copy(textureData, 0, safePtr, textureData.Length);
                bmp.UnlockBits(bmpData);


                InitializeComponent();
                PictureBoxTiles.BackgroundImage = bmp;
                PictureBoxTiles.Height = Game1.EnemyTileSheet.Height;
                PictureBoxTiles.Width = Game1.EnemyTileSheet.Width;
            }
            if (Game1.drawableLayer == 5)
            {
                byte[] textureData = new byte[4 * Game1.ScreenTileSheet.Width * Game1.ScreenTileSheet.Height];
                Game1.ScreenTileSheet.GetData<byte>(textureData);
                System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(Game1.ScreenTileSheet.Width, Game1.ScreenTileSheet.Height,
                                System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, Game1.ScreenTileSheet.Width, Game1.ScreenTileSheet.Height),
                                        System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                IntPtr safePtr = bmpData.Scan0;
                System.Runtime.InteropServices.Marshal.Copy(textureData, 0, safePtr, textureData.Length);
                bmp.UnlockBits(bmpData);


                InitializeComponent();
                PictureBoxTiles.BackgroundImage = bmp;
                PictureBoxTiles.Height = Game1.ScreenTileSheet.Height;
                PictureBoxTiles.Width = Game1.ScreenTileSheet.Width;
            }

            if (Game1.drawableLayer == 6)
            {
                byte[] textureData = new byte[4 * Game1.Back2TileSheet.Width * Game1.Back2TileSheet.Height];
                Game1.Back1TileSheet.GetData<byte>(textureData);
                System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(Game1.Back1TileSheet.Width, Game1.Back1TileSheet.Height,
                                System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, Game1.Back1TileSheet.Width, Game1.Back1TileSheet.Height),
                                        System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                IntPtr safePtr = bmpData.Scan0;
                System.Runtime.InteropServices.Marshal.Copy(textureData, 0, safePtr, textureData.Length);
                bmp.UnlockBits(bmpData);


                InitializeComponent();
                PictureBoxTiles.BackgroundImage = bmp;
                PictureBoxTiles.Height = Game1.Back1TileSheet.Height;
                PictureBoxTiles.Width = Game1.Back1TileSheet.Width;
            }
            if (Game1.drawableLayer == 7)
            {
                byte[] textureData = new byte[4 * Game1.Back2TileSheet.Width * Game1.Back2TileSheet.Height];
                Game1.Back2TileSheet.GetData<byte>(textureData);
                System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(Game1.Back2TileSheet.Width, Game1.Back2TileSheet.Height,
                                System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, Game1.Back2TileSheet.Width, Game1.Back2TileSheet.Height),
                                        System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                IntPtr safePtr = bmpData.Scan0;
                System.Runtime.InteropServices.Marshal.Copy(textureData, 0, safePtr, textureData.Length);
                bmp.UnlockBits(bmpData);


                InitializeComponent();
                PictureBoxTiles.BackgroundImage = bmp;
                PictureBoxTiles.Height = Game1.Back2TileSheet.Height;
                PictureBoxTiles.Width = Game1.Back2TileSheet.Width;
            }
            if (Game1.drawableLayer == 8)
            {
                byte[] textureData = new byte[4 * Game1.Back3TileSheet.Width * Game1.Back3TileSheet.Height];
                Game1.Back3TileSheet.GetData<byte>(textureData);
                System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(Game1.Back3TileSheet.Width, Game1.Back3TileSheet.Height,
                                System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, Game1.Back3TileSheet.Width, Game1.Back3TileSheet.Height),
                                        System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                IntPtr safePtr = bmpData.Scan0;
                System.Runtime.InteropServices.Marshal.Copy(textureData, 0, safePtr, textureData.Length);
                bmp.UnlockBits(bmpData);


                InitializeComponent();
                PictureBoxTiles.BackgroundImage = bmp;
                PictureBoxTiles.Height = Game1.Back3TileSheet.Height;
                PictureBoxTiles.Width = Game1.Back3TileSheet.Width;
            }

        }

        private void Tiles_Load(object sender, EventArgs e)
        {

        }

        private void PictureBoxTiles_Click(object sender, EventArgs e)
        {
            LocalMousePosition = PictureBoxTiles.PointToClient(Cursor.Position);

            if (Game1.drawableLayer == 0)
            {
                TileClicked = ((int)PictureBoxTiles.Width / (int)Game1.Front1TileWidth) * ((int)LocalMousePosition.Y / (int)Game1.Front1TileHeight) + ((int)LocalMousePosition.X / (int)Game1.Front1TileWidth);
                this.DialogResult = DialogResult.OK;
            }
            if (Game1.drawableLayer == 1)
            {
                TileClicked = ((int)PictureBoxTiles.Width / (int)Game1.Front2TileWidth) * ((int)LocalMousePosition.Y / (int)Game1.Front2TileHeight) + ((int)LocalMousePosition.X / (int)Game1.Front2TileWidth);
                this.DialogResult = DialogResult.OK;
            }
            if (Game1.drawableLayer == 2)
            {
                TileClicked = ((int)PictureBoxTiles.Width / (int)Game1.Front3TileWidth) * ((int)LocalMousePosition.Y / (int)Game1.Front3TileHeight) + ((int)LocalMousePosition.X / (int)Game1.Front3TileWidth);
                this.DialogResult = DialogResult.OK;
            }
            if (Game1.drawableLayer == 3)
            {
                TileClicked = ((int)PictureBoxTiles.Width / (int)Game1.Front1TileWidth) * ((int)LocalMousePosition.Y / (int)Game1.Front1TileHeight) + ((int)LocalMousePosition.X / (int)Game1.Front1TileWidth);
                this.DialogResult = DialogResult.OK;
            }
            if (Game1.drawableLayer == 4)
            {
                TileClicked = ((int)PictureBoxTiles.Width / (int)Game1.Front1TileWidth) * ((int)LocalMousePosition.Y / (int)Game1.Front1TileHeight) + ((int)LocalMousePosition.X / (int)Game1.Front1TileWidth);
                this.DialogResult = DialogResult.OK;
            }
            if (Game1.drawableLayer == 5)
            {
                TileClicked = ((int)PictureBoxTiles.Width / (int)Game1.Front1TileWidth) * ((int)LocalMousePosition.Y / (int)Game1.Front1TileHeight) + ((int)LocalMousePosition.X / (int)Game1.Front1TileWidth);
                this.DialogResult = DialogResult.OK;
            }
            if (Game1.drawableLayer == 6)
            {
                TileClicked = ((int)PictureBoxTiles.Width / (int)Game1.Front1TileWidth) * ((int)LocalMousePosition.Y / (int)Game1.Front1TileHeight) + ((int)LocalMousePosition.X / (int)Game1.Front1TileWidth);
                this.DialogResult = DialogResult.OK;
            }
            if (Game1.drawableLayer == 7)
            {
                TileClicked = ((int)PictureBoxTiles.Width / (int)Game1.Front1TileWidth) * ((int)LocalMousePosition.Y / (int)Game1.Front1TileHeight) + ((int)LocalMousePosition.X / (int)Game1.Front1TileWidth);
                this.DialogResult = DialogResult.OK;
            }
            if (Game1.drawableLayer == 8)
            {
                TileClicked = ((int)PictureBoxTiles.Width / (int)Game1.Front1TileWidth) * ((int)LocalMousePosition.Y / (int)Game1.Front1TileHeight) + ((int)LocalMousePosition.X / (int)Game1.Front1TileWidth);
                this.DialogResult = DialogResult.OK;
            }
            if (Game1.drawableLayer == 9)
            {
                TileClicked = ((int)PictureBoxTiles.Width / (int)Game1.Front1TileWidth) * ((int)LocalMousePosition.Y / (int)Game1.Front1TileHeight) + ((int)LocalMousePosition.X / (int)Game1.Front1TileWidth);
                this.DialogResult = DialogResult.OK;
            }
        }
    }
}
